package wsepForum.ui.jaxcent.states;

import java.util.Map;

import jaxcent.HtmlInputButton;
import jaxcent.JaxcentPage;
import jaxcent.Jaxception;
import wsepForum.ui.jaxcent.IXContext;

/**
 * @author Oren
 *
 */
public abstract class AXUserControl implements IXState {
	/*****************************
	 * MEMBERS                   *
	 *****************************/
	protected IXContext context;

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public AXUserControl(IXContext context) {
		this.context = context;
		initContent();
	}

	/*****************************
	 * METHODS                   *
	 *****************************/
	/* (non-Javadoc)
	 * @see wsepForum.network.server.jax.pages.states.XState#getType()
	 */
	@Override
	public StateType getType() {
		return StateType.USEREDIT;
	}

	/**
	 * inits form content
	 */
	protected void initContent() {
		construct();
		JaxcentPage page = this.context.getPage();
		new HtmlInputButton(page, "xdetailssubmit" ) {
		        @SuppressWarnings("unchecked")
				protected void onClick(Map pageData) {
		        	try {
						setEnabled(false);
						String oldButton = getValue();
						setValue("Working...");
						String newpassword = (String) pageData.get("xnewpassword");
						String retypepassword = (String) pageData.get("xpasswordconfirm");
						if (!newpassword.equals(retypepassword)) {
							context.getPage().showMessageDialog( "New and retyped password are not the same!" );
						} else if (newpassword.equals("")) {
							context.getPage().showMessageDialog( "Please enter desired password!" );
						} else {
							dealWithSubmit(pageData,newpassword);
						}
						setValue(oldButton);
						setEnabled(true);
					} catch (Jaxception e) {/*ignore a problem with disabling the button*/}
		        }
	    };
	}


	/**
	 * @param pageData
	 * @param newpassword
	 */
	@SuppressWarnings("unchecked")
	protected abstract void dealWithSubmit(Map pageData, String newpassword);


	protected void construct(String newPassStr, String title, String oldPasswordPanel,String submitText,String userField) {
		this.context.setBody(
			"<form id=\"formuserDetails\">"+
			"<div class=\"panel\"><div class=\"inner\"><span class=\"corners-top\"><span></span></span>"+
			"<h2>"+title+"</h2><fieldset><dl><dt><label >User name:</label><br /></dt>"+
			"<dd>"+ userField + "</dd>"+
			"</dl><dl><dt><label for=\"new_password\">"+newPassStr+"</label></dt>"+
			"<dd><input tabindex=\"4\" type=\"password\" name=\"xnewpassword\" id=\"xnewpassword\" size=\"30\" value=\"\" class=\"inputbox autowidth\" title=\"Password\" /></dd>"+
			"</dl><dl><dt><label for=\"password_confirm\">Retype password:</label><br /><span></span></dt>"+
			"<dd><input tabindex=\"5\" type=\"password\" name=\"xpasswordconfirm\" id=\"xpasswordconfirm\" size=\"30\" value=\"\" class=\"inputbox autowidth\" title=\"Password confirm\" />"+
			"</dd></dl></fieldset><span class=\"corners-bottom\"><span></span></span></div></div>"+
			oldPasswordPanel+ "<fieldset class=\"submit-buttons\">"+
			"<input tabindex=\"10\" type=\"button\" id=\"xdetailssubmit\" value=\""+submitText+"\" class=\"button1\" /></fieldset></form>");
	}

	/**
	 *
	 */
	protected abstract void construct();


}
